package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("hi")
    public String hi() {
        return "hi";
    }

    /**
     * http://localhost:10010/item/list?page=1&size=5
     * @param page 当前页
     * @param size 数据条数
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> list(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        log.info("[item分页查询]page: {}, size: {}", page, size);
        // 1. 构建分页构造器
        Page<Item> pageInfo = new Page<>(page, size);

        // 2. 构建条件查询器
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Item::getUpdateTime);

        // 3. 执行查询
        itemService.page(pageInfo, wrapper);

        // 4. 封装返回结果
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        itemPageDTO.setList(pageInfo.getRecords());
        itemPageDTO.setTotal(pageInfo.getTotal());
        return itemPageDTO;
    }

    @GetMapping("{id}")
    public Item getById(@PathVariable Long id) {
        // 1. 根据id查询
        Item item = itemService.getById(id);

        // 2. 返回查询结果
        return item;
    }

    /**
     * http://localhost:10010/item
     * @param item
     * @return
     */
    @PostMapping
    public String saveItem(@RequestBody Item item) {
        log.info("[item新增商品]item: {}", item);
        // 1. 保存商品
        itemService.save(item);
        return "保存成功";
    }

    /**
     * http://localhost:10010/item/status/{id}/{status}
     * @param id 商品id
     * @param status 商品状态 传1是上架 传2下架
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public String updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        log.info("[item上下架商品]id: {} status: {}", id, status);
        // 更新商品状态
        itemService.updateStatus(id, status);
        return "上下架成功";
    }

    /**
     * http://localhost:10010/item
     * @param item
     * @return
     */
    @PutMapping
    public String updateItem(@RequestBody Item item) {
        log.info("[item修改商品]item: {}", item);
        // 防止前端传入status1导致自动上架
        item.setStatus(null);
        // 1. 修改商品
        itemService.updateById(item);
        return "修改成功";
    }


    /**
     * http://localhost:10010/item/100002672307
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public String deleteItem(@PathVariable Long id) {
        log.info("[item删除商品]id: {}", id);
        // 1. 删除商品
        itemService.removeById(id);
        return "删除成功";
    }

    /**
     * http://localhost:10010/item/stock/{itemId}/{num}
     * @param itemId
     * @param num
     */
    @PutMapping("stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num) {
        itemService.deduct(itemId, num);
    }
}
